/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-10-08
 * Time: 20:35
 */


class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public class SwapPairs {
    public ListNode swapPairs(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        ListNode temp=head.next;
        ListNode prev=null;
        ListNode left=head;
        ListNode right=head.next;
        while(left!=null&&left.next!=null){
            right=left.next;
            left.next=right.next;
            right.next=left;
            if(prev!=null){
                prev.next=right;
            }
            prev=left;
            left=prev.next;
        }
        return temp;
    }
}
